<template>
  <div class="channel-edit">
    <van-cell title="我的频道">
        <van-button
          color="red"
          class="edit-btn"
          size="small"
          round plain
          type="primary"
          :border="false"
          @click="isEdit=!isEdit"
        >
        {{isEdit?'完成':'编辑'}}
        </van-button>
    </van-cell>

    <!-- 宫格标签 -->
    <van-grid :gutter="10" class="my_grid" :border="false">
      <van-grid-item
        v-for="(item,index) in channels"
        :key="item.id"
        :class="{active:index === active}"
        :icon="isEdit && index != 0 ? 'close' : ''"
        :text="item.name"
        @click="onMyClick(index,item.id)"
      />
    </van-grid>

    <!-- 推荐频道 -->
    <van-cell title="推荐频道"></van-cell>

    <!-- 宫格标签 -->
    <van-grid :gutter="10" icon-size="0.4rem" direction="horizontal" class="my_grid" :border="false">
      <van-grid-item @click="onRecommendChannels(item)" v-for="item in recommendChannels" :key="item.id" icon="plus" :text="item.name"/>
    </van-grid>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { setStorage } from '@/utils/storage.js'
import { mapState } from 'vuex'
import { addUserChannelItem, delUserChannelItem } from '@/api/user.js'
export default {
  name: 'ChannelEdit',
  data () {
    return {
      isEdit: false
    }
  },
  props: {
    channels: {
      type: Array,
      required: true
    },
    // 记录的高亮索引
    active: {
      type: Number,
      required: true
    },
    recommendChannels: {
      type: Array,
      required: true
    }
  },
  created () {

  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    // 删除数据持久化判断函数
    async deleteChannelStorage (id) {
      if (this.user) {
        Toast.loading({
          message: '加载中...',
          forbidClick: true,
          duration: 0 // 持续展示 toast
        })

        // 登录了
        try {
          await delUserChannelItem(id)
          Toast.success('删除成功')
        } catch (error) {
          Toast.fail('删除失败')
        }
      } else { // 未登录
        Toast.success('删除成功')
        // 保存到本地
        setStorage('MY-CHANNELS', this.channels)
      }
    },
    // 点击了推荐频道
    async onRecommendChannels (obj) {
      // 只是操作了页面的显示
      this.channels.push(obj)

      // 进行数据持久化
      if (this.user) {
        Toast.loading({
          message: '加载中...',
          forbidClick: true,
          duration: 0 // 持续展示 toast
        })

        // 登录了
        try {
          await addUserChannelItem({
            id: obj.id,
            seq: this.channels.length
          })
          Toast.success('添加成功')
        } catch (error) {
          Toast.fail('添加失败')
        }
      } else { // 未登录
        Toast.success('添加成功')
        // 保存到本地
        setStorage('MY-CHANNELS', this.channels)
      }
    },
    // 点击了我的频道
    onMyClick (index, id) {
      // 判断是否处于编辑状态
      if (this.isEdit) {
        if (index === 0) return
        if (index <= this.active) {
          this.$emit('setActive')
        }
        this.channels.splice(index, 1)
        // 删除数据持久化
        this.deleteChannelStorage(id)
      } else {
        // console.log('跳转')
        this.$emit('onGo', index)
      }
    }
  }
}
</script>

<style scoped lang='less'>
.channel-edit {
  // 编辑按钮样式
  .edit-btn {
    width: 104px;
    height: 48px;
  }

  // 标签宫格样式
  /deep/.my_grid{
    .van-grid-item{
      width: 160px;
      height: 86px;
      border-radius: 6px;

      .van-grid-item__content{
        background:#f4f5f6;
      }

      // 图标样式
      .van-icon-close {
        font-size: 35px;
        position: absolute;
        top: -10.5px;
        right: 5px;
      }
      .van-grid-item__icon+.van-grid-item__text {
        margin-top: 0;
      }
    }

    // active样式
    .active{
      .van-grid-item__text{
        color: red;
      }
    }
  }
}
</style>
